<script setup>
import {ref} from 'vue';
import {Close, Document} from "@element-plus/icons-vue";
import ComAffix from "@/components/ComAffix.vue";
import {getNoticesByComment, replyComment} from "@/api/notice.js";
import { useUserStore } from "@/stores/index.js";
import {userGetUserInfoService} from "@/api/user.js";
import router from "@/router/index.js";
import {humanizeTime} from "../../utiles/format.js";
const userStore = useUserStore()
// 模拟通知数据
const notifications = ref([
]);

const getNotices = async () => {
  const res = await getNoticesByComment()
  notifications.value = res.data.data
}
getNotices()

// 响应式变量
const showReplyBox = ref(false);
const currentReplyTo = ref(null);
const replyContent = ref('');

// 打开回复框
const openReply = (notification) => {
  currentReplyTo.value = notification;
  console.log("???")
  showReplyBox.value = true;
};

// 关闭回复框
const closeReply = () => {
  showReplyBox.value = false;
  replyContent.value = '';
  currentReplyTo.value = null;
};

// 提交回复
const submitReply = async () => {
  if (replyContent.value.trim()) {
    console.log('提交回复:', {
      to: currentReplyTo.value.id,
      content: replyContent.value
    });

    const comment = {
      postId: currentReplyTo.value.postId,
      userId: userStore.user.id,
      userName: userStore.user.nickname,
      userAvatar: userStore.user.avatar,
      content: replyContent.value,
      type: currentReplyTo.value.type === 0 ? 1 : 2,
      replyUserId: currentReplyTo.value.userId,
      replyUserName: currentReplyTo.value.userName,
      replyCommentId: currentReplyTo.value.id
    }
    console.log(comment)
    await replyComment(comment)
    closeReply();
  }
};

//跳转至post页
const viewPost = async (postId,userId) => {

  const res = await userGetUserInfoService(userId)
  console.log(res.data.data)
  router.push({
    name: 'Post',
    params: {
      id: postId,
    },
    query: {
      authorId: res.data.data.id,
      authorAvatar: res.data.data.avatar,
      authorName: res.data.data.nickname,
      authorIntroduced: res.data.data.introduced
    }
  })
}


</script>
<template>
  <div class="main">
    <el-affix :offset="0">
      <ComAffix></ComAffix>
    </el-affix>
    <div class="w">
      <div class="notice-nav">
        <span><router-link  :to="{ name: 'Notice' }">评论</router-link></span>
        <span><router-link  :to="{ name: 'NoticeLike' }">赞和收藏</router-link></span>
        <span><router-link  :to="{ name: 'NoticeFollow' }">新增关注</router-link></span>
      </div>
      <div class="notification-container">
        <el-card
          v-for="note in notifications"
          :key="note.id"
          shadow="hover"
          class="notification-item"
        >
          <div class="notification-header">
            <el-avatar :size="40" :src="note.userAvatar"/>
            <div class="user-info">
              <span class="nickname">{{ note.userName }}</span>
              <span class="time">{{humanizeTime(note.createTime)}}</span>
            </div>
          </div>

          <div class="notification-content">
            <!-- 回复通知 -->
            <template v-if="note.type === 1 || note.type ===2">
              <div class="reply-content">
                <p class="reply-text">{{ note.content }}</p>
                <div class="original-comment">
                  <el-divider content-position="left">回复你的评论</el-divider>
                  <p class="target-content">{{ note.targetContent }}</p>
                </div>
              </div>
            </template>

            <!-- 评论通知 -->
            <template v-else>
              <p class="comment-text">{{ note.content }}</p>
            </template>

            <div class="related-post">
              <el-icon>
                <Document/>
              </el-icon>
              <span class="post-title" @click="viewPost(note.postId,note.userId)">{{ note.postTitle }}</span>
            </div>
          </div>

          <div class="notification-actions">
            <el-button
              size="small"
              @click="openReply(note)"
            >回复
            </el-button>
          </div>
        </el-card>
        <div v-if="showReplyBox" class="reply-overlay">
          <div class="reply-box">
            <div class="reply-header">
              <h3>回复 @{{ currentReplyTo.userName }}</h3>
              <el-icon class="close-btn" @click="closeReply">
                <Close/>
              </el-icon>
            </div>

            <el-input
              v-model="replyContent"
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 6 }"
              maxlength="500"
              placeholder="写下你的回复..."
              autofocus
            />

            <div class="reply-actions">
              <el-button @click="closeReply">取消</el-button>
              <el-button type="primary" @click="submitReply">发送</el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.w {
  width: 800px;
  margin: 0 auto;
}

.notice-nav {
  display: flex;
  align-items: center;
  margin: 30px 0;
  padding: 20px 20px;
  background: white;
  border-radius: 4px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);

  span {
    margin-right: 30px;
    cursor: pointer;
    color: #666;
    padding: 8px 12px;
    border-radius: 4px;

    &.active {
      background: #f0f2f5;
      color: #409eff;
      font-weight: 500;
    }

    &:hover {
      background: #f5f7fa;
    }
  }
}

.notification-container {
  max-width: 800px;
  margin: 0 auto;
}

.notification-item {
  margin-bottom: 16px;
  transition: all 0.3s;

  &:hover {
    transform: translateX(5px);
  }
}

.notification-header {
  display: flex;
  align-items: center;
  margin-bottom: 16px;

  .user-info {
    margin-left: 12px;

    .nickname {
      font-weight: 500;
      margin-right: 8px;
    }

    .time {
      color: #909399;
      font-size: 0.85em;
    }
  }
}

.notification-content {
  margin-left: 52px;

  .reply-text, .comment-text {
    margin: 0 0 12px;
    line-height: 1.6;
    color: #303133;
  }

  .original-comment {
    background: #f8f9fa;
    border-radius: 4px;
    padding: 12px;
    margin: 12px 0;

    .target-content {
      color: #606266;
      font-size: 0.9em;
      margin: 0;
    }
  }

  .related-post {
    display: flex;
    align-items: center;
    color: #909399;
    font-size: 0.9em;

    .post-title {
      margin-left: 8px;
      color: #409eff;
      cursor: pointer;

      &:hover {
        text-decoration: underline;
      }
    }
  }
}

.notification-actions {
  margin-top: 16px;
  text-align: right;
}

.reply-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 2000;
}

.reply-box {
  background: white;
  width: 600px;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);

  .reply-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;

    h3 {
      margin: 0;
      font-size: 16px;
    }

    .close-btn {
      cursor: pointer;
      padding: 4px;

      &:hover {
        color: #409eff;
      }
    }
  }

  .reply-actions {
    margin-top: 16px;
    text-align: right;

    .el-button {
      margin-left: 12px;
    }
  }
}

// 修改原通知操作按钮样式
.notification-actions {
  margin-top: 16px;
  text-align: right;

  .el-button {
    margin-left: 8px;
  }
}

a {
  text-decoration: none;
  cursor: pointer;
  color: #636363;
}
</style>
